package nu.aing.P20140908;

import java.util.LinkedList;
import java.util.List;

import nu.aing.utils.TestUtils;

/**
 * @author memekmaut@gmail.com
 */
public class DoubleLetter {
	
	public static void main(String[] args) {
		DoubleLetter obj = new DoubleLetter();
		TestUtils.test(obj, "ableToSolve", "aabccb");
		TestUtils.test(obj, "ableToSolve", "aabccbb");
		TestUtils.test(obj, "ableToSolve", "abcddcba");
		TestUtils.test(obj, "ableToSolve", "abab");
		TestUtils.test(obj, "ableToSolve", "aaaaaaaaaa");
		TestUtils.test(obj, "ableToSolve", "aababbabbaba");
		TestUtils.test(obj, "ableToSolve", "zzxzxxzxxzzx");
	}
	
	public String ableToSolve(String S) {
		System.out.print(S + ", result=");
		List<Character> chars = new LinkedList<Character>();
		for(int i = 0; i < S.length(); i++) {
			chars.add(S.charAt(i));
		}
		return check(0, chars) ? "Possible" : "Impossible";
	}
	
	private boolean check(int i, List<Character> chars) {
		if(chars.get(i) == chars.get(++i) ) {
			chars.remove(i-1);
			chars.remove(i-1);
			i = 0;
		}
		return (chars.isEmpty() || i == chars.size() - 1) ? 
				(chars.isEmpty() ) : check(i, chars);
	}
}
